﻿using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.WechatService.Domain.WebApiEntity.CRM;
using ZilLion.Service.WechatService.Domain.WebApiRepository.InterFace;

namespace ZilLion.Service.WechatService.Domain.WebApiRepository
{
    public class AppidjoininfoRepository : Respository<AconfigItem>, IAppidjoininfoRepository
    {
        public AppidjoininfoRepository() : base(new WechatServiceContext() )
        {
        }


        public AconfigGroup GetAppidjoininfosForAdmin(string jappid)
        {
            AconfigGroup item = new AconfigGroup();
            item.jappid = jappid;
            item.ConfigItems = this.GetList("select * from xtwxappidjoininfo where jappid=@jappid and (jisforbid=0 or jisforbid=2)", new { jappid });
            item.logsn = this.GetDataByScalar<int>("select MAX(logsn) from xtdatalogcrm where logtable='xtwxappidjoininfo'").GetValueOrDefault();
            return item;
        }

        public AconfigGroup GetAppidjoininfos(string jappid)
        {
            AconfigGroup item = new AconfigGroup();
            item.jappid = jappid;
            item.ConfigItems = this.GetList("select * from xtwxappidjoininfo where jappid=@jappid and jisforbid=2", new { jappid });
            item.logsn = this.GetDataByScalar<int>("select MAX(logsn) from xtdatalogcrm where logtable='xtwxappidjoininfo'").GetValueOrDefault();
            return item;
        }

        public int CheckConfigItems()
        {
            int logsnv2 = this.GetDataByScalar<int>("select MAX(logsn) from xtdatalogcrm where logtable='xtwxappidjoininfo'").GetValueOrDefault();
            return logsnv2;
        }

        public void InsertAppidjoininfo(AconfigItem item)
        {
            this.Add(item);
        }

        public void UpdateAppidjoininfo(AconfigItem item)
        {
           //this.Execute("update xtwxappidjoininfo set jvalue=@jvalue,jisforbid=@jisforbid where jappid=@jappid and jkey=@jkey ", new { item.jisforbid, item.jvalue, item.jappid, item.jkey });
           //this.Modify(item);
        }

        public void DeleteAppidjoininfo(AconfigItem item)
        {
            //item.jisforbid = 1;
            //this.Execute("update xtwxappidjoininfo set jisforbid=1  where jappid=@jappid and jkey=@jkey", new { item.jvalue, item.jappid, item.jkey });
            //this.Modify(item);
        }

        public bool Checkappidkey(AconfigItem item)
        {
            int sn = this.GetDataByScalar<int>("select count(*)  from xtwxappidjoininfo  where jappid=@jappid and jkey=@jkey and (jisforbid=0 or jisforbid=2)", new {item.jappid, item.jkey }).GetValueOrDefault();
            return sn > 0;
        }



        public AconfigGroup GetAppids()
        {
            AconfigGroup item = new AconfigGroup();
            item.ConfigItems = this.GetList("select distinct jappid from xtwxappidjoininfo where jisforbid=0 or jisforbid=2");
            return item;
        }

        public AconfigGroup GetAppidjoininfoKey(string jappid, string jkey)
        {
            AconfigGroup item = new AconfigGroup();
            item.configItem = this.GetList("select * from xtwxappidjoininfo where jappid=@jappid and jkey=@jkey", new { jappid,jkey }).FirstOrDefault();
            return item;
        }
    }
}
